﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Npgsql;
using NpgsqlTypes;
	
namespace appLibrary
{
   partial class FrmViewQueries : Form
    {
       public string user { set; get; }
       public DataTable dtTable { set; get; }
       public string  code { set; get; }
       public string description { set; get; }
       public DataGridViewRow row { set; get; }

       public string strNameTable { set; get; }
       public string strFields { set; get; }
       public string strFieldCondicion1 {set;get;}
       public string strFieldCondicion2 { set; get; }
       public string strFieldCondicion3 { set; get; }
       public string strValueCond3 { set; get; }

       public  string codeFind { set; get; }
       public  string descFind { set; get; }
       private static int intWidth { set; get; }
       private static int intHeigth { set; get; }
       private static int intWidthTemp { set; get; }
       private static int intHeigthTemp { set; get; }

       public static int intReg { set; get; }

       //Variable de clase que contiene las columnas que se van a ocultar
       public int[] intColumnsHide { set; get; }
       //Alias para el titulo del Form
       public string strAlias { set; get; }
       
        public  FrmViewQueries()
        {
            InitializeComponent();
        }





        public void formatdgvConsulta(ref DataGridView dgvQueries, ref string strDesc, ref string strCode)
        {
            
            
            //DataGridViewCell cell = new DataGridViewTextBoxCell();

            //DataGridViewColumn columm1 = new DataGridViewColumn();
            //columm1.CellTemplate = cell;
            //DataGridViewColumn columm2 = new DataGridViewColumn();
            //columm2.CellTemplate = cell;
            if(dgvQueries.Columns[0].HeaderText.Equals("codigo"))
                dgvQueries.Columns[0].HeaderText = "Código";
            

            dgvQueries.Columns[0].Width = 80;
            //strDesc = dgvQueries.Columns[0].HeaderText.Substring(0, 1).ToUpper() + dgvQueries.Columns[0].HeaderText.Substring(1, dgvQueries.Columns[0].HeaderText.Length-1);
            //dgvQueries.Columns[0].HeaderText = strDesc;
            // dgvQueries.Columns[1].HeaderText = "Descripción";
            dgvQueries.Columns[1].Width=220;
            //if (dgvQueries.Columns.Count > 2)
            //{

            //Ocultamos columnas
            for(int i=0; intColumnsHide!=null && i<intColumnsHide.Length;i++){
                dgvQueries.Columns[intColumnsHide[0]].Visible = false;
            }


                for (int i = 0; i < dgvQueries.Columns.Count; i++)
                {
                    dgvQueries.Columns[i].HeaderText = dgvQueries.Columns[i].HeaderText.Substring(0, 1).ToUpper() + dgvQueries.Columns[i].HeaderText.Substring(1, dgvQueries.Columns[i].HeaderText.Length - 1);
                    
                    if (i == 1)
                        dgvQueries.Columns[i].Width = 180;
                    //else
                    //    dgvQueries.Columns[i].Width = 250;

                }

                strCode = dgvQueries.Columns[0].HeaderText;
                strDesc = dgvQueries.Columns[1].HeaderText;
            //}
           

            //dgvQueries.Columns.Add(columm1);
            //dgvQueries.Columns.Add(columm2);

        }

        private void FrmViewQueries_Load(object sender, EventArgs e)
        {

            intWidth = this.Width;
            intHeigth = this.Height;
            intWidthTemp = this.Width;
            intHeigthTemp = this.Height;
            this.txtCodigo.CharacterCasing = CharacterCasing.Upper;
            this.txtDescription.CharacterCasing = CharacterCasing.Upper;
            this.dgvConsulta.DataSource = this.dtTable;
            string strDesc = "",strCode="";
            formatdgvConsulta(ref dgvConsulta, ref strDesc,ref strCode);

            lblCode.Text = strCode;
            lblDesc.Text = strDesc;
            

        }

        private void dgvConsulta_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            this.showData();
        }

        


       public void  showData(){

           row = dgvConsulta.CurrentRow;


            if (dgvConsulta.RowCount > 0)
            {
                string codigo = dgvConsulta.Rows[dgvConsulta.CurrentRow.Index].Cells[0].Value.ToString();
                string description = dgvConsulta.Rows[dgvConsulta.CurrentRow.Index].Cells[1].Value.ToString(); ;
                codeFind = codigo;
                descFind = description;

                //MessageBox.Show("Datos:" + frmPoint.codFind + ", " + frmPoint.descFind);
                this.Hide();
            }
       }

       private void btnSearch_Click(object sender, EventArgs e)
       {
                                           

           DataTable table = new DataTable();
           //table.Columns.Add("Código", typeof(string));
           //table.Columns.Add("Descripción", typeof(string));

           try
           {
               DataSet dt = new DataSet();
               string sql = "";
               if (strFieldCondicion3 != null)
               {
                   if (rbCodigo.Checked)
                       if (!txtCodigo.Text.Trim().Equals(""))
                           //sql = "SELECT " + strFields + " FROM " + strNameTable + " WHERE " +
                           //    (Util.isnumeric(txtCodigo.Text) ? ("UPPER(CAST(" + strFieldCondicion1 + " as VARCHAR)) LIKE '%" + txtCodigo.Text.Trim() + "%'") :
                           // (strFieldCondicion1 + " LIKE '%" + txtCodigo.Text + "%'") + (" AND " + strFieldCondicion3 + " in(" + strValueCond3 + ")"));

                            sql = "SELECT " + strFields + " FROM " + strNameTable + " WHERE " +
                              ("UPPER(CAST(" + strFieldCondicion1 + " as VARCHAR)) LIKE '%" + txtCodigo.Text.Trim() + "%'") 
                              + (" AND " + strFieldCondicion3 + " in(" + strValueCond3 + ")");
                       else
                           sql = "SELECT " + strFields + " FROM " + strNameTable + " WHERE " + strFieldCondicion3 + " in(" + strValueCond3 + ")";

                   else
                       if (!txtDescription.Text.Trim().Equals(""))
                           sql = "SELECT " + strFields + " FROM " + strNameTable + " WHERE UPPER(" + strFieldCondicion2 + ") LIKE '%" + txtDescription.Text.Trim() + "%' AND " + strFieldCondicion3 + " in(" + strValueCond3 + ")";
                       else
                           sql = "SELECT " + strFields + " FROM " + strNameTable + " WHERE " + strFieldCondicion3 + " in(" + strValueCond3 + ")";

               }
               else
                   if (rbCodigo.Checked)
                       if (!txtCodigo.Text.Trim().Equals(""))
                           // sql = "SELECT " + strFields + " FROM " + strNameTable + " WHERE " + (Util.isnumeric(txtCodigo.Text) ?
                           // "UPPER(CAST(" + strFieldCondicion1 + " as VARCHAR))  LIKE '%" + txtCodigo.Text.Trim() + "%'" : strFieldCondicion1 + " LIKE '%" + txtCodigo.Text+"%'");
                           sql = "SELECT " + strFields + " FROM " + strNameTable + " WHERE " + "UPPER(CAST(" + strFieldCondicion1 + " as VARCHAR))  LIKE '%" + txtCodigo.Text.Trim()+"%'";
                       else
                           sql = "SELECT " + strFields + " FROM " + strNameTable;
                   else
                       if (!txtDescription.Text.Trim().Equals(""))
                           sql = "SELECT " + strFields + " FROM " + strNameTable + " WHERE UPPER( " + strFieldCondicion2 + ") like '%" + txtDescription.Text.Trim() + "%'";
                       else
                           sql = "SELECT " + strFields + " FROM " + strNameTable;


               ConnectionDB conn = new ConnectionDB();
               Log.eLog(user, "sql:" + sql);

               NpgsqlDataAdapter dtAdap = new NpgsqlDataAdapter(sql, conn.Connecction());

               dtAdap.Fill(dt, strNameTable);

               table = dt.Tables[0];
               Log.eLog(user, "Se realizó la consulta a la tabla: " + strNameTable + " satisfactoriamente");

               dgvConsulta.DataSource = table;         

              



           }
           catch (Exception ex)
           {
               Log.eLog(user, "ERROR: Se obtuvieron errores al realizar  la consulta a la tabla: " + strNameTable + ex);

           }
           finally
           {

               // conn = null;
           }
       }

       private void groupBox1_Enter(object sender, EventArgs e)
       {

       }

       private void txtCodigo_TextChanged(object sender, EventArgs e)
       {
           btnSearch_Click(sender, e);
       }

       private void txtDescription_TextChanged(object sender, EventArgs e)
       {
           btnSearch_Click(sender, e);
       }

       private void txtCodigo_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
       {
           rbCodigo.Checked = true;
       }

       private void txtDescription_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
       {
           rbDescription.Checked = true;
       }

       private void FrmViewQueries_Resize(object sender, EventArgs e)
       {
           //if (this.Width < 373 || this.Height < 398)
           //{
           //    this.Width = 373;
           //    this.Height = 398;
           //    dgvConsulta.Width = 341;
           //    dgvConsulta.Height=227;
           //    return;
           //}

          
           
           //dgvConsulta.Width = this.Width -50;
           //dgvConsulta.Height = this.Height - 200;
           //gpbFilter.Width = this.Width - 40;

           //if (intWidth < this.Width)
           //{

           //    for (int i = 1; i < dgvConsulta.Columns.Count; i++)
           //    {
           //        dgvConsulta.Columns[i].Width = dgvConsulta.Columns[i].Width + Convert.ToInt32((this.Width-intWidthTemp)*0.05);
           //    }
           //}
           //else
           //{
           //    for (int i = 1; i < dgvConsulta.Columns.Count; i++)
           //    {
           //        dgvConsulta.Columns[i].Width = dgvConsulta.Columns[i].Width - Convert.ToInt32((this.Width - intWidthTemp) * 0.05);
           //    }

           //}
           
           //    intWidth = this.Width;
           //    intHeigth = this.Height;

           
       }

       private void FrmViewQueries_Activated(object sender, EventArgs e)
       {
           txtCodigo.Focus();
       }

       private void tspbAccept_Click(object sender, EventArgs e)
       {
           this.showData();
       }

       private void btnCancel_Click(object sender, EventArgs e)
       {
           this.Hide();
       }

       private void tspbCancelDetail_Click(object sender, EventArgs e)
       {
           this.Hide();
       }

       private void FrmViewQueries_ResizeBegin(object sender, EventArgs e)
       {
           //if (this.Width < 373 || this.Height < 398)
           //{
           //    return;
           //}
       }

       private void dgvConsulta_CellEnter(object sender, DataGridViewCellEventArgs e)
       {
           //tspbAccept_Click(sender, e);
       }

       private void txtDescription_KeyPress(object sender, KeyPressEventArgs e)
       {

       }

       private void txtDescription_KeyDown(object sender, KeyEventArgs e)
       {
           //if (e.KeyCode == Keys.Enter)
           //{
           //    dgvConsulta.Focus();
           //}
       }

       private void txtCodigo_KeyDown(object sender, KeyEventArgs e)
       {
           if (e.KeyCode == Keys.Enter)
           {
               txtDescription.Focus();
           }
       }

       
    }
}
